package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.mapper.EbCardInfoMapper;
import cn.com.chengmandian.bike.model.EbCardInfo;
import cn.com.chengmandian.bike.model.EbIdentityInfo;
import cn.com.chengmandian.bike.model.vo.CardInfoVo;
import cn.com.chengmandian.bike.service.IEbCardInfoService;
import cn.com.chengmandian.bike.service.IEbIdentityInfoService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 骑行卡信息列表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-11
 */
@Service
public class EbCardInfoServiceImpl extends ServiceImpl<EbCardInfoMapper, EbCardInfo> implements IEbCardInfoService {

    @Autowired
    private IEbIdentityInfoService identityInfoService;

    @Override
    public List<EbCardInfo> getInfoList(Integer areaId) {
        Date now = new Date();
        return this.list(new LambdaQueryWrapper<EbCardInfo>()
                .eq(EbCardInfo::getAreaId, areaId)
                .eq(EbCardInfo::getIsEnabled, CommonConstant.ONE)
                .eq(EbCardInfo::getIsDeleted, CommonConstant.ZERO)
                .le(EbCardInfo::getSalesStartTime, now)
                .ge(EbCardInfo::getSalesEndTime, now)
                .orderByAsc(EbCardInfo::getSort));
    }

    @Override
    public EbCardInfo getInfo(Integer cardId) {
        Date now = new Date();
        return this.getOne(new LambdaQueryWrapper<EbCardInfo>()
                .eq(EbCardInfo::getIsEnabled, CommonConstant.ONE)
                .eq(EbCardInfo::getIsDeleted, CommonConstant.ZERO)
                .le(EbCardInfo::getSalesStartTime, now)
                .ge(EbCardInfo::getSalesEndTime, now)
                .eq(EbCardInfo::getId, cardId));
    }

    @Override
    public CardInfoVo detail(Integer id) {
        CardInfoVo vo = baseMapper.detail(id);
        if(StrUtil.isNotBlank(vo.getIdentityLimit())) {
            List<EbIdentityInfo> identityInfos = identityInfoService.list(new LambdaQueryWrapper<EbIdentityInfo>()
                    .in(EbIdentityInfo::getId, Arrays.asList(vo.getIdentityLimit().split(",")))
                    .last(" order by sort asc, id desc"));
            vo.setIdentityInfo(identityInfos);
        }
        return vo;
    }
}
